package com.chushouya.controller.manager.clerk;

import com.chushouya.common.annotations.AuthSkip;
import com.chushouya.common.annotations.AuthValidate;
import com.chushouya.common.constant.ServiceType;
import com.chushouya.common.support.Contexts;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.chushouya.manager.dto.admin.clerk.ClerkInfoDTO;
import com.chushouya.manager.dto.admin.signin.ClerkSignInDTO;
import com.chushouya.manager.service.api.ClerkApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 业务员接口
 */
@RestController
@RequestMapping(ServiceType.CLERK + "/clerk")
@Api(tags = ServiceType.CLERK + "业务员接口")
@Slf4j
public class ClerkApiController {

    @Resource
    private ClerkApiService clerkApiService;

    @ApiOperation("业务员注册")
    @PostMapping(value = "/sign")
    @AuthSkip
    public Response<Long> signIn(@RequestBody @Valid ClerkSignInDTO signIn) {
        clerkApiService.signIn(signIn);
        return Response.success();
    }

    @ApiOperation("业务员信息")
    @GetMapping("/clerkInfo")
    @AuthValidate
    public Response<ClerkInfoDTO> clerkInfo() {
        log.info("=============>获取业务员信息, clerkId: {}", Contexts.getUserContext());
        return Response.success(clerkApiService.getClerkInfo(Contexts.getUserContext().getClerkId()));
    }

    @PostMapping("/editWorkStatus")
    @ApiOperation("业务员工作状态:接单/休息")
    @Log(businessType = BusinessType.UPDATE)
    public Response<Void> editWorkStatus(Integer workStatus) {
        final Long clerkId = Contexts.getUserContext().getClerkId();
        clerkApiService.editWorkStatus(clerkId, workStatus);
        return Response.success();
    }

    @ApiOperation("保存微信用户信息")
    @PostMapping("/saveWxNickNameAndAvatar")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nickName", value = "微信昵称", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "avatar", value = "微信头像", dataType = "String", paramType = "query", required = true)
    })
    @AuthValidate
    public Response<ClerkInfoDTO> saveWxNickNameAndAvatar(String nickName, String avatar) {
        return Response.success(clerkApiService.saveWxNickNameAndAvatar(Contexts.getUserContext().getClerkId(), nickName, avatar));
    }


}
